package com.hnit.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hnit.web.bean.CouponGrabRecords;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Param;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 抢券记录Mapper接口
 */
@Mapper
public interface CouponGrabRecordsMapper extends BaseMapper<CouponGrabRecords> {

    /**
     * 统计用户某个优惠券的抢购次数
     * @param userId 用户ID
     * @param couponId 优惠券ID
     * @return 抢购次数
     */
    @Select("SELECT COUNT(*) FROM coupon_grab_records " +
            "WHERE userId = #{userId} AND couponId = #{couponId}")
    Long countByUserIdAndCouponId(@Param("userId") Long userId, @Param("couponId") Long couponId);

    /**
     * 统计某个优惠券的成功抢购次数
     * @param couponId 优惠券ID
     * @return 成功次数
     */
    @Select("SELECT COUNT(*) FROM coupon_grab_records " +
            "WHERE couponId = #{couponId} AND grabResult = 1")
    Long countSuccessByGroupId(@Param("couponId") Long couponId);

    /**
     * 查询指定时间范围内的抢购记录
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param couponId 优惠券ID（可选）
     * @return 抢购记录列表
     */
    @Select("<script>" +
            "SELECT * FROM coupon_grab_records " +
            "WHERE grabTime BETWEEN #{startTime} AND #{endTime} " +
            "<if test='couponId != null'>" +
            "AND couponId = #{couponId} " +
            "</if>" +
            "ORDER BY grabTime DESC" +
            "</script>")
    List<CouponGrabRecords> selectByTimeRange(@Param("startTime") LocalDateTime startTime, 
                                              @Param("endTime") LocalDateTime endTime,
                                              @Param("couponId") Long couponId);

    /**
     * 统计各种抢购结果的数量
     * @param couponId 优惠券ID
     * @return 统计结果 [成功数, 库存不足数, 重复抢购数, 其他失败数]
     */
    @Select("SELECT " +
            "SUM(CASE WHEN grabResult = 1 THEN 1 ELSE 0 END) as successCount, " +
            "SUM(CASE WHEN grabResult = 2 THEN 1 ELSE 0 END) as noStockCount, " +
            "SUM(CASE WHEN grabResult = 3 THEN 1 ELSE 0 END) as duplicateCount, " +
            "SUM(CASE WHEN grabResult > 3 THEN 1 ELSE 0 END) as otherFailCount " +
            "FROM coupon_grab_records WHERE couponId = #{couponId}")
    Object[] countResultStatistics(@Param("couponId") Long couponId);

    /**
     * 查询指定IP地址的抢购记录
     * @param ipAddress IP地址
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 抢购记录列表
     */
    @Select("SELECT * FROM coupon_grab_records " +
            "WHERE ipAddress = #{ipAddress} AND grabTime BETWEEN #{startTime} AND #{endTime} " +
            "ORDER BY grabTime DESC")
    List<CouponGrabRecords> selectByIpAndTime(@Param("ipAddress") String ipAddress,
                                              @Param("startTime") LocalDateTime startTime,
                                              @Param("endTime") LocalDateTime endTime);

    /**
     * 统计某个时间段内同一IP的抢购次数
     * @param ipAddress IP地址
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 抢购次数
     */
    @Select("SELECT COUNT(*) FROM coupon_grab_records " +
            "WHERE ipAddress = #{ipAddress} AND grabTime BETWEEN #{startTime} AND #{endTime}")
    Long countByIpAndTime(@Param("ipAddress") String ipAddress,
                          @Param("startTime") LocalDateTime startTime,
                          @Param("endTime") LocalDateTime endTime);
}
